package com.download.wangkeke.config;


import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Order(-1)
public class AopLog {


  private String  level;
  private String type;


  public AopLog() {
    System.out.println("Aop");
  }

  public AopLog(String level, String type) {
    this.level = level;
    this.type = type;
    System.out.println(level+type);
  }

  /**
   * 切点
   */
  @Pointcut("@annotation(com.download.wangkeke.config.MethodLog)")
  public void methodCachePointcut() { }


  /**
   * 切面
   *
   * @param point
   * @return
   * @throws Throwable
   */
  @Around("methodCachePointcut()")
  public Object around(ProceedingJoinPoint point) throws Throwable {

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
    Calendar ca = Calendar.getInstance();
    String operDate = df.format(ca.getTime());
    System.out.println("operDate"+operDate);
    String methodRemark = getMthodRemark(point);
    System.out.println("methodRemark"+methodRemark);
    String methodName = point.getSignature().getName();
    System.out.println("methodName"+methodName);
    String packages = point.getThis().getClass().getName();
    if (packages.indexOf("$$EnhancerByCGLIB$$") > -1) { // 如果是CGLIB动态生成的类
      try {
        packages = packages.substring(0, packages.indexOf("$$"));
        System.out.println(packages);
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }
    String operatingcontent = "";
    Object[] method_param = null;

    Object object;
    try {
      method_param = point.getArgs(); //获取方法参数
      System.out.println("method_param"+method_param);
      Object param= point.proceed(point.getArgs());
      System.out.println(param);
      System.out.println("param"+param);
      return param;
    } catch (Exception e) {
      // 异常处理记录日志..log.error(e);
      throw e;
    }

  }

  /**
   * 方法异常时调用
   *
   * @param ex
   */
  @AfterThrowing(pointcut = "methodCachePointcut()",throwing = "ex")
  public void afterThrowing(Exception ex) {
    System.out.println("afterThrowing");
    System.out.println(ex);
  }

  /**
   * 获取方法中的中文备注
   *
   * @param joinPoint
   * @return
   * @throws Exception
   */
  public static String getMthodRemark(ProceedingJoinPoint joinPoint) throws Exception {

    String targetName = joinPoint.getTarget().getClass().getName();
    String methodName = joinPoint.getSignature().getName();
    Object[] arguments = joinPoint.getArgs();

    Class targetClass = Class.forName(targetName);
    Method[] method = targetClass.getMethods();
    String methode = "";
    for (Method m : method) {
      if (m.getName().equals(methodName)) {
        Class[] tmpCs = m.getParameterTypes();
        if (tmpCs.length == arguments.length) {
          MethodLog methodCache = m.getAnnotation(MethodLog.class);
          if (methodCache != null) {
            methode = methodCache.methodDesc();
          }
          break;
        }
      }
    }
    return methode;
  }
}
